package com.example.weather_news.http;


import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.weather_news.App;
import com.example.weather_news.R;

import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/09.
 */

public class HttpRequest {
    private static final String APP_KEY = "ef8a557075201bf1";

    //查询城市天气
    public static void requestWeather(String cityName, Response.Listener<JSONObject> listener,
                                      Response.ErrorListener errorListener) {
        if (App.context == null) {
            Log.e("context********", "");
            return;
        }
        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        String url = " http://api.jisuapi.com/weather/query?appkey=" + APP_KEY + "&city=" +
                cityName;
        JsonObjectRequest objectRequest;
        if (listener == null) {
            listener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                }
            };
        }
        if (errorListener == null) {
            errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };
        }

        objectRequest = new JsonObjectRequest(Request.Method.POST,url, listener, errorListener);
        mQueue.add(objectRequest);
    }

    //查询全国城市
    public static void requestCity(Response.Listener<JSONObject> listener,
                                   Response.ErrorListener errorListener) {
        if (App.context == null) {
            return;
        }
        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        String url = "http://api.jisuapi.com/weather/city?appkey=" + APP_KEY;
        JsonObjectRequest objectRequest;
        if (listener == null) {
            listener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                }
            };
        }
        if (errorListener == null) {
            errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };
        }

        objectRequest = new JsonObjectRequest(Request.Method.POST,url, listener, errorListener);
        mQueue.add(objectRequest);
    }

    //查询新闻标题
    public static void requestNewsChannel(Response.Listener<JSONObject> listener,
                                          Response.ErrorListener errorListener) {
        if (App.context == null) {
            return;
        }
        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        String url = "http://api.jisuapi.com/news/channel?appkey=" + APP_KEY;
        JsonObjectRequest objectRequest;
        if (listener == null) {
            listener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                }
            };
        }
        if (errorListener == null) {
            errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };
        }

        objectRequest = new JsonObjectRequest(Request.Method.POST,url, listener, errorListener);
        mQueue.add(objectRequest);
    }

    //查询新闻内容
    public static void requestNews(String channel, int start, Response.Listener<JSONObject> listener,
                                   Response.ErrorListener errorListener) {
        if (App.context == null) {
            return;
        }
        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        String url = "http://api.jisuapi.com/news/get?channel=" + channel +
                "&start="+start+"&num=20&appkey=" + APP_KEY;
        JsonObjectRequest objectRequest;
        if (listener == null) {
            listener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                }
            };
        }
        if (errorListener == null) {
            errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };
        }

        objectRequest = new JsonObjectRequest(Request.Method.POST,url, listener, errorListener);
        mQueue.add(objectRequest);
    }

    //搜所新闻
    public static void requestSearchNews(String keyword, Response.Listener<JSONObject> listener,
                                         Response.ErrorListener errorListener) {
        if (App.context == null) {
            return;
        }
        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        String url = "http://api.jisuapi.com/news/search?keyword=" + keyword + "&appkey=" + APP_KEY;
        JsonObjectRequest objectRequest;
        if (listener == null) {
            listener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {

                }
            };
        }
        if (errorListener == null) {
            errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };
        }

        objectRequest = new JsonObjectRequest(url, listener, errorListener);
        mQueue.add(objectRequest);
    }

    //获取网络图片
    public static void requestImg(String url, final ImageView img){

        RequestQueue mQueue = Volley.newRequestQueue(App.context);
        ImageRequest ir=new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                img.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                img.setImageResource(R.mipmap.ic_launcher_round);
            }
        });
        mQueue.add(ir);
    }

}
